package org.example.spring.cycle.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author hzq
 * @date 2022/3/12 13:49
 */
//@Component
public class ExampleInit implements EnvironmentAware, ApplicationContextAware, InitializingBean, ApplicationRunner {

    public ExampleInit() {
        System.out.println("ExampleInit -- 构造函数");
    }

    public String getBeanName() {
        return "exampleInit -- 实例在ioc容器中的名称";
    }

    public void setEnvironment(Environment environment) {
        System.out.println("ExampleInit -- setEnvironment");
//        String[] activeProfiles = environment.getActiveProfiles();
//        Stream.of(activeProfiles).forEach(System.out::println);
//        String[] defaultProfiles = environment.getDefaultProfiles();
//        Stream.of(defaultProfiles).forEach(System.out::println);
//        System.out.println(environment.getProperty("server.port"));
    }

    public void afterPropertiesSet() throws Exception {
        System.out.println("ExampleInit -- afterPropertiesSet");
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("ExampleInit -- setApplicationContext");
    }

    @PostConstruct
    public void init() {
        System.out.println("ExampleInit -- init");
    }

    // 程序启动后执行
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("ExampleInit -- run");
    }
}
